{
  "cells": [
    {
      "attachments": {},
      "cell_type": "markdown",
      "id": "96832bc2",
      "metadata": {},
      "source": [
        "[![](https://baihai.co/img/IDP-logo003.png)](https://idp.baihai.co/)"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {
        "id": "4de72369-cde3-4a4c-bc8a-46e602fe68ff",
        "index": 1
      },
      "source": [
        "## Install Pytorch package"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": 3,
      "metadata": {
        "id": "a8492acb-e8b1-4483-af07-dc789ac9ecd6",
        "index": 2
      },
      "outputs": [],
      "source": [
        "!pip install torch torchvision"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {
        "id": "3b2019e7-16cc-4fbb-86f8-973abd43bde3",
        "index": 3
      },
      "source": [
        "## Install some related packages required for stable diffusion"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": 6,
      "metadata": {
        "id": "37af2389-33c1-4d57-8a06-a99cd67d9e4d",
        "index": 4
      },
      "outputs": [],
      "source": [
        "!pip install --upgrade diffusers transformers accelerate"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {
        "id": "1b89d98f-8b38-45d7-9544-c9be056b846d",
        "index": 5
      },
      "source": [
        "## Download the Stable diffusion files"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": 3,
      "metadata": {
        "id": "178f8fcb-db73-47f2-bc17-d297e7fb640a",
        "index": 6
      },
      "outputs": [],
      "source": [
        "!brew install git-lfs\n",
        "!git lfs install\n",
        "!git clone https://huggingface.co/stabilityai/stable-diffusion-2"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {
        "id": "6efa3c94-8010-4b7a-bdd1-2dc454b04877",
        "index": 7
      },
      "source": [
        "## Generate and show an AIGC image"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": 5,
      "metadata": {
        "id": "324b68a5-2d3d-40d3-a12e-8f20d919e2da",
        "index": 8
      },
      "outputs": [],
      "source": [
        "from diffusers import StableDiffusionPipeline\n",
        "\n",
        "pipe = StableDiffusionPipeline.from_pretrained(\"stable-diffusion-2\")\n",
        "# disable the following line if you run on CPU\n",
        "#pipe = pipe.to(\"cuda\")\n",
        "prompt = \"a photo of an astronaut riding a horse on mars\"\n",
        "pipe.enable_attention_slicing()\n",
        "image = pipe(prompt, num_inference_steps=20).images[0]  \n",
        "import matplotlib.pyplot as plt\n",
        "plt.imshow(image)\n",
        "plt.axis('off')\n",
        "plt.show()"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {
        "id": "49b4f360-d9bb-4345-992d-a43d1dae363b",
        "index": 9
      },
      "source": [
        "## Control the generated image by setting multiple parameters"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": 11,
      "metadata": {
        "id": "02f7f45f-e4be-43a6-a333-aaab28faa696",
        "index": 10
      },
      "outputs": [],
      "source": [
        "prompt = \" a santa claus, portrait photography, 8k \"\n",
        "n_propmt = \"bad, deformed, ugly, bad anotomy\"\n",
        "image = pipe(prompt=prompt,\n",
        "             negative_prompt=n_propmt, \n",
        "             strength=0.7, \n",
        "             num_inference_steps=20).images[0]\n",
        "plt.imshow(image)\n",
        "plt.axis('off')\n",
        "plt.show()"
      ]
    }
  ],
  "metadata": {
    "kernelspec": {
      "display_name": "Python 3 (IdpKernel)",
      "language": "python",
      "name": "python3"
    },
    "language_info": {
      "codemirror_mode": {
        "name": "ipython",
        "version": 3
      },
      "file_extension": ".py",
      "mimetype": "text/x-python",
      "name": "python",
      "nbconvert_exporter": "python",
      "pygments_lexer": "ipython3",
      "version": "3.9.7"
    }
  },
  "nbformat": 4,
  "nbformat_minor": 5
}
